package com.atguigu.base;

import com.atguigu.util.CastUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public abstract class  BaseServiceImpl<T> implements BaseService<T> {

    public abstract BaseDao<T> getEntityDao();


    @Override
    public Integer insert(T t) {
        return getEntityDao().insert(t);
    }

    @Override
    public T getById(Serializable id) {
        return getEntityDao().getById(id);
    }

    @Override
    public Integer update(T t) {
        return getEntityDao().update(t);
    }

    @Override
    public void delete(Serializable id) {
        getEntityDao().delete(id);
    }

    @Override
    public PageInfo<T> findPage(Map<String, Object> filters) {
        int pageNum = CastUtil.castInt(filters.get("pageNum"),1);
        int pageSize = CastUtil.castInt(filters.get("pageSize"),5);
        //开启分页功能，将这两个参数，与当前的线程进行绑定，传递给dao层
        //select 语句最后自动增加 limit ?,?  limit startIndex ,pageSize
        PageHelper.startPage(pageNum, pageSize);
        Page<T> page = getEntityDao().findPage(filters);
        return new PageInfo(page,5);
    }
}
